<?php 

class Register extends CI_Controller {

	public function __construct()
    {
        parent::__construct();
        $this->load->model('film');
        $this->load->model('registermodel');
        $this->load->model('loginmodel');
    }

	public function index()
	{
		$logat = $this->loginmodel->este_logat();
		$data['logat'] = $logat;
		if($logat == 1)
		{
			redirect('');
		}
		$this->form_validation->set_rules('parola', 'Parola', 'required|min_length[4]|alpha_numeric|trim');
		$this->form_validation->set_rules('nume', 'Nume', 'required|min_length[2]|trim');
		$this->form_validation->set_rules('prenume', 'Prenume', 'required|min_length[2]|trim');
		$this->form_validation->set_rules('email', 'Email', 'required|min_length[6]|valid_email|trim');			
		if ($this->form_validation->run() == FALSE)
		{
			$data['titlu'] = 'Star Cinema | Inregistrare';
			$this->load->view('home/header', $data);
			$this->load->view('register/register');
		}
		else 
		{ 
			$email = $this->input->post('email');
			if($this->registermodel->exista($email) == TRUE)
			{
				$data['titlu'] = 'Star Cinema | Inregistrare';
				$this->load->view('home/header', $data);
				$data['exista'] = 'Adresa de e-mail aleasă este deja folosită. Vă rugăm să alegeți alta.';
				$this->load->view('register/register', $data);
			}
			else
			{
				$nume = $this->input->post('nume');
				$prenume = $this->input->post('prenume');
				$parola = $this->input->post('parola');
				$email = $this->input->post('email');																			
				$this->registermodel->insereaza($parola, $nume, $prenume, $email);
				$data['titlu'] = 'Star Cinema | Inregistrare succes';
				$this->load->view('home/header', $data);
				$this->load->view('register/succes');
			}
		}
		$this->load->view('home/footer');
	}
}

?>